package com.employeemanagementsystem.controller;

import com.employeemanagementsystem.dao.FeedbackDao;
import com.employeemanagementsystem.dao.UserDao;
import com.employeemanagementsystem.model.Feedback;
import com.employeemanagementsystem.model.User;

import jakarta.servlet.http.HttpSession;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin_home")
public class AdminHomeServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user != null && "ADMIN".equalsIgnoreCase(user.getRole())) {
            UserDao userDao = new UserDao();
            List<User> users = userDao.getAllUsers();

            FeedbackDao feedbackDao = new FeedbackDao();
            List<Feedback> pendingFeedbacks = feedbackDao.getPendingFeedbacks();
            List<Feedback> processedFeedbacks = feedbackDao.getProcessedFeedbacks(); // 获取已处理的反馈

            request.setAttribute("users", users);
            request.setAttribute("pendingFeedbacks", pendingFeedbacks);
            request.setAttribute("processedFeedbacks", processedFeedbacks); // 将已处理的反馈传递给 JSP
            request.getRequestDispatcher("/WEB-INF/jsp/admin_home.jsp").forward(request, response);
        } else {
            response.sendRedirect("login.jsp?error=Please log in as an administrator.");
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int feedbackId = Integer.parseInt(request.getParameter("feedbackId"));
        String responseContent = request.getParameter("response");

        FeedbackDao feedbackDao = new FeedbackDao();
        if (feedbackDao.updateFeedbackStatus(feedbackId, responseContent)) {
            response.sendRedirect(request.getContextPath() + "/admin_home?message=反馈处理成功！");
        } else {
            response.sendRedirect(request.getContextPath() + "/admin_home?error=反馈处理失败，请重试。");
        }
    }
}
